package schedule.configuration;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import schedule.task.QuartzJob;

import static org.quartz.SimpleScheduleBuilder.simpleSchedule;

/**
 * https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-quartz.html
 */
@Configuration
public class QuartzConfigurations {
    @Bean
    public JobDetail jobDetail() {
        return JobBuilder.newJob().ofType(QuartzJob.class)
                .storeDurably()
                .withIdentity("Quartz_Job_Detail")
                .withDescription("Invoke Quartz Job...")
                .build();
    }

    /**
     * another way to use job detail
     * @return
     */
    /*@Bean
    public JobDetailFactoryBean jobDetail() {
        JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
        jobDetailFactory.setJobClass(QuartzJob.class);
        jobDetailFactory.setDescription("invoke quartz job which created by factory...");
        jobDetailFactory.setDurability(true);
        return jobDetailFactory;
    }*/

    @Bean
    public Trigger trigger(JobDetail job) {
        return TriggerBuilder.newTrigger().forJob(job)
                .withIdentity("Quartz_Trigger")
                .withDescription("Quartz trigger")
                .withSchedule(simpleSchedule().repeatForever().withIntervalInSeconds(5))
                .build();
    }

    /**
     * another way to use trigger
     * @return
     */
    /*@Bean
    public SimpleTriggerFactoryBean trigger(JobDetail job) {
        SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
        trigger.setJobDetail(job);
        trigger.setRepeatInterval(5000);
        trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
        return trigger;
    }*/
}
